﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Globalization;
using System.Net;
using HtmlAgilityPack;

namespace FilesTubeClient
{
    public class ResultItem
    {
        private XmlDocument _item_node = null;

        //--------------------------------------------------------------------------------
        private int _id = 0;
        public int ID
        {
            get { return _id; }
        }
        //--------------------------------------------------------------------------------
        private string _filename = null;
        public string Filename
        {
            get { return _filename; }
        }
        //--------------------------------------------------------------------------------
        private string _extension = null;
        public string Extension
        {
            get { return _extension; }
        }
        //--------------------------------------------------------------------------------
        private int _size = 0;
        public int Size
        {
            get { return _size; }
        }
        //--------------------------------------------------------------------------------
        private string _description = null;
        public string Description
        {
            get { return _description; }
        }
        //--------------------------------------------------------------------------------
        private Uri _filetube_file_address_page = null;
        public Uri FilesTubeFileAddressPage
        {
            get { return _filetube_file_address_page; }
        }
        //--------------------------------------------------------------------------------
        private Uri _filetube_file_details_page = null;
        public Uri FilesTubeFileDetailsPage
        {
            get { return _filetube_file_details_page; }
        }
        //--------------------------------------------------------------------------------
        private List<string> _tags = new List<string>();
        public string[] Tags
        {
            get { return _tags.ToArray(); }
        }
        //--------------------------------------------------------------------------------
        private double _rate = 0.0;
        public double Rate
        {
            get { return _rate; }
        }
        //--------------------------------------------------------------------------------
        private DateTime _added = new DateTime();
        public DateTime Added
        {
            get { return _added; }
        }
        //--------------------------------------------------------------------------------
        private string _related = null;
        public string Related
        {
            get { return _related; }
        }
        //--------------------------------------------------------------------------------
        private string _password = null;
        public string Password
        {
            get { return _password; }
        }
        //--------------------------------------------------------------------------------
        private List<Uri> _link_url = new List<Uri>();
        public Uri[] LinkUrl
        {
            get { return _link_url.ToArray(); }
        }
        //--------------------------------------------------------------------------------
        public ResultItem(XmlNode itemNode)
        {
            _item_node = new XmlDocument();
            _item_node.LoadXml(itemNode.OuterXml);
            parse();
        }
        //--------------------------------------------------------------------------------
        private void parse()
        {
            _id = int.Parse(_item_node.SelectSingleNode("//hits[@id]").Attributes["id"].InnerText);
            _filename = _item_node.SelectSingleNode("//hits/name").InnerText;
            _extension = _item_node.SelectSingleNode("//hits/extension").InnerText;
            _description = _item_node.SelectSingleNode("//hits/description").InnerText;
            _filetube_file_address_page = new Uri(_item_node.SelectSingleNode("//hits/address").InnerText.Trim());
            _filetube_file_details_page = new Uri(_item_node.SelectSingleNode("//hits/details").InnerText.Trim());
            _tags.AddRange(_item_node.SelectSingleNode("//hits/details").InnerText.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries));
            _rate = double.Parse(_item_node.SelectSingleNode("//hits/rate").InnerText);
            _added = DateTime.ParseExact(_item_node.SelectSingleNode("//hits/added").InnerText, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
            _related = _item_node.SelectSingleNode("//hits/related").InnerText;
            _password = _item_node.SelectSingleNode("//hits/password").InnerText;
            parse_size();
            find_link_url();
        }
        //--------------------------------------------------------------------------------
        private void parse_size()
        {
            string filesize = _item_node.SelectSingleNode("//hits/size").InnerText;
            string[] splitFilesize = filesize.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            _size = int.Parse(splitFilesize[0]);
            splitFilesize[1] = splitFilesize[1].ToLower();
            if (splitFilesize[1].StartsWith("m"))
                _size += 1000000;
            else if (splitFilesize[1].StartsWith("g"))
                _size += 1000000000;
        }
        //--------------------------------------------------------------------------------
        private void find_link_url()
        {
            WebClient wc = new WebClient();
            string downloadPage = wc.DownloadString(FilesTubeFileDetailsPage);

            HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
            html.LoadHtml(downloadPage);
            HtmlNodeCollection links = html.DocumentNode.SelectNodes("//pre[@id='copy_paste_links']");
            if (links.Count != 1)
                throw new Exception(string.Format("Expected only 1 iframe, found {0}", links.Count));

            string[] strlinks = links[0].InnerText.Split(new string[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
            foreach (string l in strlinks)
                _link_url.Add(new Uri(l));
        }
        //--------------------------------------------------------------------------------
    }
}
